//
//  RxTestCellDifferController.swift
//  DemoProject
//
//  Created by Hao on 2018/8/22.
//  Copyright © 2018年 hao. All rights reserved.
//

/// RxSwift tableview 和 collectionView 范例: http://www.hangge.com/blog/cache/category_72_4.html

import UIKit

class RxTestCellDifferController: BaseViewController {

    let disposeBag = DisposeBag()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        let tableView = UITableView(frame: self.view.bounds, style: .plain)
        self.view.addSubview(tableView)
        tableView.top = kStatusBarAndNaviHeight()
        tableView.height = self.view.height-tableView.top
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "titleImageCell")
        
        //初始化数据
        let sections = Observable.just([
            MySection(header: "我是第一个分区", items: [
                .TitleImageSectionItem(title: "图片数据1", image: UIImage(named: "image_php")!),
                .TitleImageSectionItem(title: "图片数据2", image: UIImage(named: "image_react")!),
                .TitleSwitchSectionItem(title: "开关数据1", enabled: true)
                ]),
            MySection(header: "我是第二个分区", items: [
                .TitleSwitchSectionItem(title: "开关数据2", enabled: false),
                .TitleSwitchSectionItem(title: "开关数据3", enabled: false),
                .TitleImageSectionItem(title: "图片数据3", image: UIImage(named: "image_swift")!)
                ])
            ])
        
        //创建数据源
        let dataSource = RxTableViewSectionedReloadDataSource<MySection>(
            //设置单元格
            configureCell: { dataSource, tableView, indexPath, item in
                switch dataSource[indexPath] {
                case let .TitleImageSectionItem(title, image):
                    let cell = tableView.dequeueReusableCell(withIdentifier: "titleImageCell")!
//                                                             for: indexPath)
                    cell.textLabel?.text = title
                    cell.imageView?.image = image
//                    (cell.viewWithTag(1) as! UILabel).text = title
//                    (cell.viewWithTag(2) as! UIImageView).image = image
                    return cell
                    
                case let .TitleSwitchSectionItem(title, _):
                    let cell = tableView.dequeueReusableCell(withIdentifier: "titleImageCell")!
//                                                             for: indexPath)
                    cell.textLabel?.text = title
//                    (cell.viewWithTag(1) as! UILabel).text = title
//                    (cell.viewWithTag(2) as! UISwitch).isOn = enabled
                    return cell
                }
        },
            //设置分区头标题
            titleForHeaderInSection: { ds, index in
                return ds.sectionModels[index].header
        }
        )
        
        //绑定单元格数据
        sections
            .bind(to: tableView.rx.items(dataSource: dataSource))
            .disposed(by: disposeBag)
    }
    
}

// 单元格类型
fileprivate enum SectionItem {
    case TitleImageSectionItem(title: String, image: UIImage)
    case TitleSwitchSectionItem(title: String, enabled: Bool)
}

// 自定义Section
fileprivate struct MySection {
    var header: String
    var items: [SectionItem]
}

extension MySection : SectionModelType {
    typealias Item = SectionItem
    
    init(original: MySection, items: [Item]) {
        self = original
        self.items = items
    }
}


